package com.usian.student.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.usian.common.pojo.ResultVo;
import com.usian.common.pojo.UserTo;
import com.usian.student.dao.StudentDao;
import com.usian.student.feign.LoginFeignService;
import com.usian.student.feign.RoomFeignService;
import com.usian.student.pojo.Room;
import com.usian.student.pojo.Student;
import com.usian.student.service.StudentService;
import com.usian.third.uitils.VerifyRealName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Map;
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private StudentDao studentDao;

    @Autowired
    private RoomFeignService roomFeignService;

    @Autowired
    private LoginFeignService loginFeignService;
    @Transactional
    @Override
    public ResultVo register(Student student) throws IOException {
//        判断三要素是否为一个人 姓名+手机号+身份证号
//        VerifyRealName verifyRealName=new VerifyRealName();
//        Map result=verifyRealName.checkUser(student.getCard(),student.getPhone(),student.getName());
        if (true){
//                请求接口成功
            if (true){
                 //信息一致
                //判断手机验证码是否正确,
//                验证流程
//                    1、验证redis中的验证码是否过期
//                    2、验证判断用户输入的验证码和redis中的验证码是否一致
                String redisCode=redisTemplate.opsForValue().get(student.getPhone());
                if (!StrUtil.isEmpty(redisCode)){
                if (redisCode.equals(student.getCode())){
                    //判断重复注册
                    LambdaQueryWrapper<Student>lambdaQueryWrapper=new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(Student::getPhone,student.getPhone());
                    long count=studentDao.selectCount(lambdaQueryWrapper);
                    if (count>0){
//                        手机号已经被注册
                        return new ResultVo(300,"该手机号已经被注册",null);
                    }else {
//                        需要根据对应的班级id来获取学生的项目经理id和班主任id
                        //用户未注册,可以直接保存到数据库
                        ResultVo resultVo = roomFeignService.info(student.getCid());
                        Room room = JSON.parseObject(JSON.toJSONString(resultVo.getData()), Room.class);
                         student.setHid(room.getHid());
                         student.setTid(room.getTid());
                        studentDao.insert(student);
                        UserTo userTo=new UserTo(student.getName(),student.getPhone(),5);
                        loginFeignService.save(userTo);
                        return new ResultVo(200,"注册成功",null);
                    }
                }else {
                    return new ResultVo(301,"验证码输入错误",null);
                }
                }else {
                    return new ResultVo(300,"验证码过期，请重新发送",null);
                }
            }else {
                return new ResultVo(300,"异常",null);
            }
        }else {
//                请求接口异常
            return new ResultVo();
        }
    }
}
